//
//  MainTabBar.m
//  Weibo
//
//  Created by APPLE on 2/15/16.
//  Copyright © 2016 renzc. All rights reserved.
//

#import "MainTabBar.h"

@interface MainTabBar ()

@property (nonatomic) UIButton *plusButton;

@end

@implementation MainTabBar

#pragma mark - 懒加载

- (UIButton *)plusButton {
    if (_plusButton == nil) {
        _plusButton = [[UIButton alloc] init];
        [_plusButton setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState: UIControlStateNormal];
        [_plusButton setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
        [_plusButton setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
        [_plusButton setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
        [_plusButton sizeToFit];
    }
    return _plusButton;
}

#pragma mark - 初始化

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    return self;
}

- (void)layoutSubviews {
    NSInteger index = 0;
    CGFloat width = self.bounds.size.width / 5;
    NSLog(@"%f",width);
    [super layoutSubviews];
    for (UIView *view in self.subviews) {
        index ++;
        //自定义tabbar控件，通过遍历。
        if ([view isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            if (index >= 4) {
                view.frame = CGRectMake(width * (index -1), 0, width, self.bounds.size.height);
            }else {
                view.frame = CGRectMake(width * (index - 2), 0, width, self.bounds.size.height);
            }
            [self addSubview: self.plusButton];
            self.plusButton.frame = CGRectMake(width * 2, 0, width, self.bounds.size.height);
            NSLog(@"%lu",index);
        }
    }
}

@end
